﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OEMS
{
    /// <summary>
    /// Instellingen backup
    /// </summary>
    /// <author>Nick Lauryssen</author>
    ///
    public partial class BackupForm : Form
    {
        private static string backup_path;
        private static string main_path;
        private static DateTime last_backup;

        public BackupForm()
        {
            InitializeComponent();

            last_backup = Properties.Settings.Default.LastBackup;
            main_path = Properties.Settings.Default.SourceFolder;
            backup_path = Properties.Settings.Default.DestFolder;

            if (main_path.Equals(""))
            {
                main_path = System.IO.Directory.GetCurrentDirectory() + "\\OemsDB.mdf";
            }

            if (last_backup.Year != 0001)
            lblLast.Text = "Previous backup: " + last_backup;

            txtPath2.Text = main_path;
            txtPath.Text = backup_path;
            cboTime.SelectedIndex = Properties.Settings.Default.BackupSeq;

            if (Properties.Settings.Default.PerformAsk == false)
            {
                label2.Enabled = true;
                cboTime.Enabled = true;
                checkBox1.Checked = false;
            }
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if (fbdBrowser.ShowDialog() == DialogResult.OK)
            {
                backup_path = fbdBrowser.SelectedPath + "\\OemsDB.mdf";
                txtPath.Text = backup_path;
                Properties.Settings.Default.DestFolder = backup_path;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Reload();
            this.Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            this.Dispose();
        }

        private void btnBrowse2_Click(object sender, EventArgs e)
        {
            if (fbdBrowser.ShowDialog() == DialogResult.OK)
            {
                main_path = fbdBrowser.SelectedPath + "\\OemsDB.mdf";
                txtPath2.Text = main_path;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                Properties.Settings.Default.PerformAsk = true;
                label2.Enabled = true;
                cboTime.Enabled = true;
            }
            else
            {
                Properties.Settings.Default.PerformAsk = false;
                label2.Enabled = false;
                cboTime.Enabled = false;
            }
        }

        private void cboTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.BackupSeq = cboTime.SelectedIndex;
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            executeFile("backup.bat",main_path, backup_path);
            last_backup = DateTime.Now;
            Properties.Settings.Default.LastBackup = last_backup;
            lblLast.Text = "Previous backup: " + last_backup;
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            string oldLoc = Properties.Settings.Default.SourceFolder;

            if (main_path != null)
            {
                main_path = main_path.Substring(0, main_path.Length - 11);
                executeFile("moveDir.bat", oldLoc, main_path);
                oldLoc = main_path + "\\OemsDB.mdf";
                
            }
            else
            {
                main_path = oldLoc;
                executeFile("backup.bat", backup_path, main_path);
            }
            Properties.Settings.Default.SourceFolder = oldLoc;
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            if (main_path != "" && backup_path != "")
            {
                executeFile("recover.bat", backup_path, main_path.Substring(0, main_path.Length - 11));
            }
            else
            {
                MessageBox.Show("Backup not successfull.\n Please enter destination and source folder.");
            }
        }
        /// <summary>
        /// Voert een bestand uit voor een backup/recover te maken
        /// </summary>
        /// <param name="file">Bevat de naam van de te uitvoeren file</param>
        /// <param name="loc1">Bevat het source path</param>
        /// <param name="loc2">Bevat het destination path</param>
        /// <author>Nick Lauryssen</author>
        ///
        public static void executeFile(string file, string loc1, string loc2)
        {
            if (loc1 != "" && loc2 != "")
            {
                try
                {
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = file;
                    p.StartInfo.Arguments = "\"" + loc1 + "\" \"" + loc2 + "\"";
                    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    p.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Please choose a destination folder in the Settings.");
            }
        }

           
    }
}
